package DataStuctures.LinkList;
/*
* 链表的删除
* */
public class LinkListDummyHead_Delete<E> {

    private class Node{
        public  E e;
        public Node next;

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }

        public Node(E e){
            this(e,null);
        }

        public Node(){
            this(null,null);
        }

        @Override
        public String toString() {
            return e.toString();
        }
    }

    private Node dummyHead;
    int size;


    public LinkListDummyHead_Delete(){
        dummyHead = new Node(null,null);
        size = 0;
    }

    //返回链表中的元素个数
    public int getSize(){
        return size;
    }


    //返回链表是否为空
    public boolean isEmpty(){
        return size == 0;
    }


    //在链表的index位置添加新的元素e
    public void add(int index,E e){

        if(index < 0 || index >size){
            throw new IllegalArgumentException("Add Failed,Illegal index");
        }

        Node prev = dummyHead;
        //遍历prev 把prev指向要加入位置的前一个元素
                        //⬇要记忆的边界值
        for (int i = 0; i < index; i++) {  //找的是插入位置的前一个元素，所以要-1 but现在多了个dummyhead 还要+1
            prev = prev.next;   //一直向前移动，直到到了index-1的位置
        }

        prev.next = new Node(e, prev.next);
        size++;

    }

    //在链表表头添加新的元素
    public void addFirst(E e){
        add(0,e);
    }

    public void addLast(E e){
        add(size,e);
    }


    //获得元素
    public E get(int index){
        if(index < 0 || index >=size){
            throw new IllegalArgumentException("Add Failed,Illegal index");
        }

        Node cur = dummyHead.next;   //从链表的第一个元素开始遍历
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        return cur.e;
    }


    //获取链表第一个元素
    public E getFirst(){
        // return dummyHead.next.e;  //-->笨蛋思路
         return get(0);
    }

    //获取链表最后一个元素
    public E getLast(){
        return get(size - 1);
    }

    //修改链表的第index个元素的位置e
    //在链表中不是常用的操作，练习用
    public void set(int index,E e){
        if(index < 0 || index >=size){
            throw new IllegalArgumentException("Set Failed,Illegal index");
        }
       /* E cur = get(index);
        cur = e;*/   //逻辑上貌似没问题

        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        cur.e = e;
    }

    //查找链表中是否存在元素e
    public boolean contains(E e){

        Node cur = dummyHead.next;
        //可以用for循环 遍历size
        while(cur != null){
            if(cur.e.equals(e)){
                return true;
            }
            //如果不等于，遍历
            cur = cur.next;
        }
        return false;
    }

    //删除元素
    public E remove(int index){
        if(index < 0 || index >=size){
            throw new IllegalArgumentException("Remove Failed,Illegal index");
        }
        Node prev = dummyHead; //从虚拟节点开始
        for (int i = 0; i < index; i++) {
            prev = prev.next;  //这样就存了待删除之前的节点
        }
        Node retNode = prev.next;   //retNode拿的是删节点
        prev.next = retNode.next;
        retNode.next = null;
        size --;
        return retNode.e;
    }

    public E removeFirst(){
        /*
        Node retNode = dummyHead.next;
        dummyHead.next = retNode.next;
        retNode.next = null;
        */
        return remove(0);
    }


    public E removeLast(){
        return remove(size - 1);
    }


    @Override
    public String toString() {

        StringBuilder res = new StringBuilder();
        Node cur = dummyHead.next;
        while (cur != null){
            res.append(cur + "->");
            cur = cur.next;
        }
        /*
        *
        for (Node cur = dummyHead.next; cur != null ; cur = cur.next) {
            res.append(cur + "->");
        }
        * */
        res.append("NULL");
        return res.toString();
    }
}
